﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ULSLogParser;

namespace SharePointManager
{
    public partial class ListFilterBuilder : Form
    {
        public ListFilterBuilder()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void buttonAddEditFilterRule_Click(object sender, EventArgs e)
        {
            TreeNode SelectedNode = treeViewFilterData.SelectedNode;
            if (SelectedNode != null)
            {
                FilterRule newfilterRule = CreateFilerRule(comboBoxColumn.SelectedItem.ToString(),
                                                           comboBoxOperator.SelectedItem.ToString(),
                                                           textBoxValue.Text);
                if (radioButtonSingle.Checked)
                {
                    TreeNode newNode = new TreeNode(newfilterRule.ToString());
                    newNode.Tag = newfilterRule;
                    SelectedNode.Nodes.Add(newNode);
                }
                else if (radioButtonAnd.Checked)
                {
                    TreeNode AndNode = new TreeNode("And");
                    TreeNode newNode = new TreeNode(newfilterRule.ToString());
                    newNode.Tag = newfilterRule;
                    AndNode.Nodes.Add(newNode);
                    SelectedNode.Nodes.Add(AndNode);
                }
                else
                {
                    TreeNode AndNode = new TreeNode("Or");
                    TreeNode newNode = new TreeNode(newfilterRule.ToString());
                    newNode.Tag = newfilterRule;
                    AndNode.Nodes.Add(newNode);
                    SelectedNode.Nodes.Add(AndNode);
                }
            }
        }
        private FilterRule CreateFilerRule(string ColumnName, string Operator, string Value)
        {
            FilterRule newRule = null;
            Op Operation = Op.Equal;
            switch (Operator)
            {
                case "< (Less Than)":
                    Operation = Op.LessThan;
                    break;
                case "> (Greater Than)":
                    Operation = Op.GreaterThan;
                    break;
                case "<= (Less Than Or Equal)":
                    Operation = Op.LessThanOrEqual;
                    break;
                case ">= (Greater Than Or Equal)":
                    Operation = Op.GreaterThanOrEqual;
                    break;
                case "<> (Not Equal to)":
                    Operation = Op.NotEqual;
                    break;
                default:
                    Operation = Op.Equal;
                    break;
            }
            newRule = new FilterRule(ColumnName, Operation, Value);
            return newRule;
        }
        private void buttonOk_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            if (treeViewFilterData.Nodes[0].Nodes.Count > 0 &&
            MessageBox.Show("You are about to remove all the filter data. Do you want to continue?",
                            "Filter Building Removing", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes)
            {
                this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
                this.Close();
                return;
            }
            else if (treeViewFilterData.Nodes[0].Nodes.Count <= 0)
            {
                this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
                this.Close();
            }

        }

        private void treeViewFilterData_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (treeViewFilterData.SelectedNode != null)
            {
                TreeNode SelectedNode = treeViewFilterData.SelectedNode;
                buttonAddEditFilterRule.Enabled = true;
                if (SelectedNode.Text != "Filter Root")
                {
                }
            }
            else
            {
                buttonAddEditFilterRule.Enabled = false;
            }
        }
    }
}
